/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Samplingprotocol;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Eventelement;
import java.util.ArrayList;
import java.util.List;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class SamplingprotocolJpaController {

    public SamplingprotocolJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public Samplingprotocol save(Samplingprotocol samplingprotocol) {
        Samplingprotocol aux = findSamplingprotocol(samplingprotocol.getSamplingprotocol());
        if(aux==null)
            return create(samplingprotocol);
        else
            return aux;
    }

    public Samplingprotocol create(Samplingprotocol samplingprotocol) {
        Samplingprotocol r = null;
        if (samplingprotocol.getEventelementList() == null) {
            samplingprotocol.setEventelementList(new ArrayList<Eventelement>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Eventelement> attachedEventelementList = new ArrayList<Eventelement>();
            for (Eventelement eventelementListEventelementToAttach : samplingprotocol.getEventelementList()) {
                eventelementListEventelementToAttach = em.getReference(eventelementListEventelementToAttach.getClass(), eventelementListEventelementToAttach.getIdeventelement());
                attachedEventelementList.add(eventelementListEventelementToAttach);
            }
            samplingprotocol.setEventelementList(attachedEventelementList);
            r = em.merge(samplingprotocol);
            for (Eventelement eventelementListEventelement : samplingprotocol.getEventelementList()) {
                Samplingprotocol oldSamplingprotocolOfEventelementListEventelement = eventelementListEventelement.getSamplingprotocol();
                eventelementListEventelement.setSamplingprotocol(samplingprotocol);
                eventelementListEventelement = em.merge(eventelementListEventelement);
                if (oldSamplingprotocolOfEventelementListEventelement != null) {
                    oldSamplingprotocolOfEventelementListEventelement.getEventelementList().remove(eventelementListEventelement);
                    oldSamplingprotocolOfEventelementListEventelement = em.merge(oldSamplingprotocolOfEventelementListEventelement);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return r;
    }

    public void edit(Samplingprotocol samplingprotocol) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Samplingprotocol persistentSamplingprotocol = em.find(Samplingprotocol.class, samplingprotocol.getIdsamplingprotocol());
            List<Eventelement> eventelementListOld = persistentSamplingprotocol.getEventelementList();
            List<Eventelement> eventelementListNew = samplingprotocol.getEventelementList();
            List<Eventelement> attachedEventelementListNew = new ArrayList<Eventelement>();
            for (Eventelement eventelementListNewEventelementToAttach : eventelementListNew) {
                eventelementListNewEventelementToAttach = em.getReference(eventelementListNewEventelementToAttach.getClass(), eventelementListNewEventelementToAttach.getIdeventelement());
                attachedEventelementListNew.add(eventelementListNewEventelementToAttach);
            }
            eventelementListNew = attachedEventelementListNew;
            samplingprotocol.setEventelementList(eventelementListNew);
            samplingprotocol = em.merge(samplingprotocol);
            for (Eventelement eventelementListOldEventelement : eventelementListOld) {
                if (!eventelementListNew.contains(eventelementListOldEventelement)) {
                    eventelementListOldEventelement.setSamplingprotocol(null);
                    eventelementListOldEventelement = em.merge(eventelementListOldEventelement);
                }
            }
            for (Eventelement eventelementListNewEventelement : eventelementListNew) {
                if (!eventelementListOld.contains(eventelementListNewEventelement)) {
                    Samplingprotocol oldSamplingprotocolOfEventelementListNewEventelement = eventelementListNewEventelement.getSamplingprotocol();
                    eventelementListNewEventelement.setSamplingprotocol(samplingprotocol);
                    eventelementListNewEventelement = em.merge(eventelementListNewEventelement);
                    if (oldSamplingprotocolOfEventelementListNewEventelement != null && !oldSamplingprotocolOfEventelementListNewEventelement.equals(samplingprotocol)) {
                        oldSamplingprotocolOfEventelementListNewEventelement.getEventelementList().remove(eventelementListNewEventelement);
                        oldSamplingprotocolOfEventelementListNewEventelement = em.merge(oldSamplingprotocolOfEventelementListNewEventelement);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = samplingprotocol.getIdsamplingprotocol();
                if (findSamplingprotocol(id) == null) {
                    throw new NonexistentEntityException("The samplingprotocol with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Samplingprotocol samplingprotocol;
            try {
                samplingprotocol = em.getReference(Samplingprotocol.class, id);
                samplingprotocol.getIdsamplingprotocol();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The samplingprotocol with id " + id + " no longer exists.", enfe);
            }
            List<Eventelement> eventelementList = samplingprotocol.getEventelementList();
            for (Eventelement eventelementListEventelement : eventelementList) {
                eventelementListEventelement.setSamplingprotocol(null);
                eventelementListEventelement = em.merge(eventelementListEventelement);
            }
            em.remove(samplingprotocol);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Samplingprotocol> findSamplingprotocolEntities() {
        return findSamplingprotocolEntities(true, -1, -1);
    }

    public List<Samplingprotocol> findSamplingprotocolEntities(int maxResults, int firstResult) {
        return findSamplingprotocolEntities(false, maxResults, firstResult);
    }

    private List<Samplingprotocol> findSamplingprotocolEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Samplingprotocol.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Samplingprotocol findSamplingprotocol(String q) {
        Query query= getEntityManager().createNamedQuery("Samplingprotocol.findBySamplingprotocol");
        query.setParameter("samplingprotocol",q);

        return query.getResultList().isEmpty()?null:(Samplingprotocol)query.getResultList().get(0);
    }

    public Samplingprotocol findSamplingprotocol(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Samplingprotocol.class, id);
        } finally {
            em.close();
        }
    }

    public int getSamplingprotocolCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Samplingprotocol> rt = cq.from(Samplingprotocol.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
